<?php

Yii::import('common.models._base.BaseTrainerProfileReview');

class TrainerProfileReview extends BaseTrainerProfileReview {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function getAllReviewTrainnerprofile($userId) {
        $criteria = new CDbCriteria;
        $criteria->addCondition('to_user_id='.$userId);       
        $criteria->order = 'RAND()';
        $criteria->limit = 2;           
        return $reviewdetails = TrainerProfileReview::model()->findAll($criteria);
    }

    public static function trainerResponseRate($type, $classCourseId) {
        $criteria = new CDbCriteria;
        $criteria->condition = $type . '=' . $classCourseId;
        $totalEnqury = count(MakeQuery::model()->findAll($criteria));
        if ($totalEnqury >= 1) {
            $totalReplyEnqury = count(MakeQuery::model()->findAll($type = $classCourseId . ' AND mail_sent=1'));
            $totalResponse = ($totalReplyEnqury / $totalEnqury) * 100;
            return ceil($totalResponse) . ' %';
        } else {
            return'No Query !';
        }
    }

    public static function savetrainerReviewAfterloginuser() {
        $trainerData = Yii::app()->session['trainer_profile_review'];
        $model = new TrainerProfileReview;
        $loginuser = getLoginUserId();
        if ($loginuser != $trainerData['userId']) {
            $model->setAttribute('reviews_text', $trainerData['text']);
            $model->setAttribute('from_user_id', $loginuser);
            $model->setAttribute('create_time', SkillConfig::CreateDateTime());
            $model->setAttribute('status', SkillConfig::STATUS_ACTIVE);
            if ($trainerData['userType'] == 'backend') {
                $type = 'backend';
                $model->setAttribute('backend_user_id', $trainerData['userId']);
                $reviewsUser = $model->findByAttributes(array('to_user_id' => $trainerData['userId'], 'backend_user_id' => $loginuser));
                if ($reviewsUser <> "" && ($reviewsUser->backend_user_id == $loginuser)) {
                    $errorCode = Message::repeat_user;
                }
            } else {
                $type = 'frontend';
                $model->setAttribute('to_user_id', $trainerData['userId']);
                $reviewsUser = $model->findByAttributes(array('to_user_id' => $trainerData['userId'], 'from_user_id' => $loginuser));
                if ($reviewsUser <> "" && ($reviewsUser->from_user_id == $loginuser)) {
                    $errorCode = Message::repeat_user;
                }
            }
            if ($model->save(FALSE)) {
                $errorCode = Message::success;
            }
        }
        if ($loginuser == $trainerData['userId']) {
            $errorCode = Message::owan_user;
        }
        unset(Yii::app()->session['trainer_profile_review']);
        return Yii::app()->createAbsoluteUrl('TrainerProfileReview/trainerprofilereview', array('type' => $type, 'skillusers' => $trainerData['userId'], 'fromuserid' => $loginuser, 'skillcode' => $errorCode));
    }

}